<template>
  <div id="testListBatchHandle">
    <!-- Form-开始 -->
    <el-dialog :title="`【${handleObj.name}】操作`" width="500px" v-model="dialogFormVisible" destroy-on-close draggable>
      <el-form ref="pagepanelnameForm" :model="mainInfo" label-width="80px" class="form-input-m baseForm">
        <el-row v-for="(m,i) in form.range" :key="i">
          <el-col :span="10">
            <el-form-item label="标本号[起]">
              <el-input v-model="m.s" name="sampleNo" clearable style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="标本号[止]">
              <el-input v-model="m.e" name="sampleNo" clearable style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="4" class="flex justify-center">
            <vxe-button v-if="i>0" type="text" content="" icon="vxe-icon-delete-fill" class-name="padding-lr-xs" class="button-red padding-lr-xs" @click="deleteRange(m,i)" />
          </el-col>
        </el-row>
        <el-form-item v-if="isEditSuccess">
          <span class="text-bold text-green margin-right-xs">
            <vxe-icon name="vxe-icon-success-circle-fill" roll></vxe-icon>
          </span>
          <span class="text-bold text-green">{{`修改成功`}}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <vxe-button type="text" :disabled="showLoading" class="button-white padding-lr-xs margin-right-xs" @click="closeDialog()" content="取消"></vxe-button>
          <vxe-button type="text" :disabled="showLoading" content="增加号段" class-name="padding-lr-xs margin-right-xs" class="button-blue margin-right-xs padding-lr-xs" @click="addRange" />
          <vxe-button :loading="showLoading" :disabled="showLoading" type="text" class="button-blue padding-lr-xs" @click="confirm()" content="确认"> </vxe-button>
        </div>
      </template>
    </el-dialog>
    <!-- Form-结束 -->
    <!-- 静默打印 -->
    <QuietPrint ref="quietPrintRef" :printParams="printParamsObj" />
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, getCurrentInstance, watch, onMounted } from 'vue';
import { pageSizes, DateToString, formatDateTime, IsEmpty } from '@/utils/common';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { VXETable } from 'vxe-table';
import XEUtils from 'xe-utils';
import { AppStore } from '@/stores/store';
import QuietPrint from '@/components/QuietPrint.vue';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();
let showLoading = ref(false);
let dialogFormVisible = ref(false);
let props = defineProps({}) as any;
let mainInfo: any = reactive({});
let beforeEdit = ref('');
let isEditSuccess = ref(null);
let form = reactive({ range: [{ s: '', e: '' }] });
let handleObj: any = reactive({ code: '', name: '' });
let printParamsObj = ref(null);
let sampleda = ref('');
let rptGroup = ref('');
let testType = ref(1); //检验类型 1-常规检验 2-微生物检验
defineExpose({
  initPage
})
onMounted(() => {
  //todo
});

/**接收父组件传过来的弹框展开状态
 * @param row 当前修改行
 * @param handleObj 右键菜单对象  事件源
 * */
function initPage(row: any, menuObj, da, rptGrp,type) {
  if (!IsEmpty(menuObj)) {
    Object.assign(handleObj, menuObj);
  }
  else {
    Object.assign(handleObj, { code: '', name: '' });
  }
  testType.value = type;
  sampleda.value = da;
  rptGroup.value = rptGrp;
  Object.assign(form, { range: [{ s: '', e: '' }] });
  isEditSuccess.value = false;
  dialogFormVisible.value = true;
  proxy.$forceUpdate();
}

/**关闭窗体 */
function closeDialog() {
  dialogFormVisible.value = false;
}
/**删除号段 
 * @param m 删除项
 * @param i 删除的索引
*/
function deleteRange(m, i) {
  form.range.splice(i, 1);
}

/**新增号段 */
function addRange() {
  form.range.push({ s: '', e: '' });
}

/**确认操作 */
function confirm() {

  let url = '';
  let data = {} as any;
  let actflag = 1;/**回调时 区分 是更新数据 还是刷新列表 */
  if (handleObj.code === 'DeleteMany') {
    if (testType.value === 1) {
      url = '/lab/maininfo/labMainInfoBatchDelete';
    }else{
      url = '/lab/maininfoBac/labMainInfoBatchDelete';
    }
    data = { sampleda: sampleda.value, rptGroup: rptGroup.value, range: form.range };
    actflag = 2;
  }
  if (handleObj.code === 'FirstReviewMany') {
    url = '/lab/maininfo/labMainInfoBatchChk';
    data = { sampleda: sampleda.value, rptGroup: rptGroup.value, range: form.range, chkValue: 1, chkDoctor: '', chkDoctorNa: '' };
    if (store.chkDoctorObj) {
      data.chkDoctor = store.chkDoctorObj.code;
      data.chkDoctorNa = store.chkDoctorObj.name;
    }
  }
  if (handleObj.code === 'ReviewMany') {
    url = '/lab/maininfo/labMainInfoBatchChk';
    data = { sampleda: sampleda.value, rptGroup: rptGroup.value, range: form.range, chkValue: 2, chkDoctor: '', chkDoctorNa: '' };
    if (store.chkDoctorObj) {
      data.chkDoctor = store.chkDoctorObj.code;
      data.chkDoctorNa = store.chkDoctorObj.name;
    }
    data.caType = window.apiUrl.CA_TYPE;
  }
  if (handleObj.code === 'PrintMany') {
    url = '/lab/maininfo/labMainInfoBatchPrint';
    data = { sampleda: sampleda.value, rptGroup: rptGroup.value, range: form.range };
  }
  isEditSuccess.value = false;
  showLoading.value = true;
  proxy.$http
    .post(url, data)
    .then((res: any) => {
      if (res && res.status && res.status.code !== 1) {
        VxeMessage(res.status.msg, VxeNotifyType.error);
      } else if (res && res.status && res.status.code === 1) {
        if (res.data && res.data.labMaininfoList && res.data.labMaininfoList.length > 0) {
          isEditSuccess.value = true;
          proxy.$emit('uptSuccess', res.data.labMaininfoList, actflag);
        }
        if (handleObj.code === 'PrintMany') {
          if (res.data && res.data.labRptmainList && res.data.labRptmainList.length > 0) {
            if (res.data.labRptmainList && res.data.labRptmainList.length > 0) {
              let arr = [];
              for (let e of res.data.labRptmainList) {
                arr.push({ ReportName: e.rptTemplateCode, ReportParameters: [{ Name: 'Id', Value: e.id }], PdfPrinterSetting: { PdfPrinterCopies: 1 } });
              }
              printParamsObj.value = arr; //[{ ReportName: '1759924571139497986', ReportParameters: [{ Name: 'Id', Value: '123' }], PdfPrinterSetting: { PdfPrinterCopies: 1 } }]; // { id: new Date().valueOf(), params: ['123', '345', '567'] };
            }
          }
        }
      } else {
        VxeMessage('请求错误', VxeNotifyType.error);
      }
      showLoading.value = false;
      setTimeout(() => {
        closeDialog();
      }, 1000);

    })
    .catch((err: any) => {
      showLoading.value = false;
      VxeMessage('批量操作失败,请联系管理员', VxeNotifyType.error);
    });
}

</script>
<style lang="scss" scope>
#testListBatchHandle {
  height: 100%;
}
</style>
